// pages/makeImg/index.js
const app = getApp()
var styles = [
	{
		transformOrigin: '100% 50%',
		transform: 'translateX(-45vw) scale(0.9)',
		opacity: '0.5',
		zIndex: 1
	},
	{
		transformOrigin: '',
		transform: 'translateX(20vw)',
		opacity: '1',
		zIndex: 3
	},
	{
		transformOrigin: '',
		transform: 'translateX(85vw) scale(0.9)',
		opacity: '0.5',
		zIndex: 2
	},
	{
		transformOrigin: '',
		transform: 'translateX(100vw) scale(0.9)',
		opacity: '0',
		zIndex: 2
	}
];
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		startX: '',
		swiperImgs: [
			{
				src: 'http://wechat-program.maggie.vip/images/bian/template-android.png',
				styles: styles[0]
			},
			{
				src: 'http://wechat-program.maggie.vip/images/bian/template-iphone.png',
				styles: styles[1]
			},
			{
				src: 'http://wechat-program.maggie.vip/images/bian/template-android.png',
				styles: styles[2]
			},
			{
				src: 'http://wechat-program.maggie.vip/images/bian/template-iphone.png',
				styles: styles[3]
			}
		],
		nowPage: 1,
		height:'',//图片轮播区高度
		tokenNum:1, //要设置的token数量
		time:'12:30', //表单中的时间
		shade:"none",
		headImg:"http://wechat-program.maggie.vip/images/default.png",
		upImgType:"headImg", //上传图片类型，默认是头像 头像 headImg, logo logo
		tokenLogo:[],
		tokenLogoNth:0, //操作的是第几个的tokenLogo
		isVip:false,
		userInfo:{},
		tokenName:{},
		tokenPrice:{}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		var query = wx.createSelectorQuery()
		query.selectAll('.swiperImg').boundingClientRect()
		let that = this;
		query.exec(function (res) {	
			var height = res[0][1].height;
			var windowWidth = wx.getSystemInfoSync().windowWidth;
			that.setData({
				height: 1334*0.6*(windowWidth/750)+ 'px'
			});
		})
		let nowTime = Math.round(new Date().getTime() / 1000).toString();
		//微信登录
		app.login(function (userInfo) {
			that.setData({
				userInfo: userInfo.data
			})
			//查询是否是vip了
			if (userInfo.data.vipEndTime > nowTime || userInfo.data.vipEndTime == 'ever') {
				that.setData({
					isVip: true
				});
				if (userInfo.data.vipEndTime != 'ever') {
					that.setData({
						startTime: app.format(userInfo.data.vipStartTime),
						endTime: app.format(userInfo.data.vipEndTime)
					});
				}
			} else {
				that.setData({
					isVip: false
				});
				wx.showModal({
					title: '提示',
					content: 'vip项目需要购买vip后才可以使用',
					showCancel:false,
					success:function(){
						wx.redirectTo({
							url: '../vip/buy'
						})
					}
				})
			}
		});
		this.getTokenPrice({
			detail: { value: 'ETH' },
			target: {
				dataset: {
					index: 0
				}
			}
		});
	},
	/**
	 * 截图类型选择区开始移动
	 */
	startMove: function (options) {
		var startX = options.changedTouches[0].pageX;
		this.setData({
			startX: startX
		});
	},
	/**
	 * 截图类型选择区结束移动
	 */
	endMove: function (options) {
		var endX = options.changedTouches[0].pageX;
		var moveX = endX - this.data.startX;
		//向右移动的情况下
		if (moveX > 20) {
			if (this.data.nowPage <= 1) {
				this.setData({
					nowPage: 4
				});
			} else {
				this.setData({
					nowPage: this.data.nowPage - 1
				});
			}
		}
		//向左移动的情况下
		else if (moveX < -20) {
			if (this.data.nowPage > 3) {
				this.setData({
					nowPage: 1
				});
				console.log("小于1");
			} else {
				this.setData({
					nowPage: this.data.nowPage + 1
				});

			}
		}
		this.checkPage(this.data.nowPage);
		console.log(this.data.nowPage);
	},
	checkPage: function (index) {
		let that = this;
		var swiperImgs = that.data.swiperImgs;
		if (index == 1) {
			swiperImgs[0].styles = styles[0];
			swiperImgs[1].styles = styles[1];
			swiperImgs[2].styles = styles[2];
			swiperImgs[3].styles = styles[3];
		} else if (index == 2) {
			swiperImgs[0].styles = styles[3];
			swiperImgs[1].styles = styles[0];
			swiperImgs[2].styles = styles[1];
			swiperImgs[3].styles = styles[2];
		} else if (index == 3) {
			swiperImgs[0].styles = styles[2];
			swiperImgs[1].styles = styles[3];
			swiperImgs[2].styles = styles[0];
			swiperImgs[3].styles = styles[1];
		} else if (index == 4) {
			swiperImgs[0].styles = styles[1];
			swiperImgs[1].styles = styles[2];
			swiperImgs[2].styles = styles[3];
			swiperImgs[3].styles = styles[0];
		}
		that.setData({
			swiperImgs: swiperImgs,
		});
	},
	bindTimeChange:function(options){
		this.setData({
			time: options.detail.value
		});	
	},
	/**
	 * 添加头像
	 */
	addHeadImg:function(option){
		this.setData({
			shade:'block',
			upImgType: option.currentTarget.dataset.type
		});
	
		if (option.target.dataset.index != undefined) {
			this.setData({
				tokenLogoNth: option.currentTarget.dataset.index 
			});
		}
	},
	hiddenShade:function(){
		this.setData({
			shade: 'none'
		});
	},
	/**
	 * 增加一个token设置选项
	 */
	addToken:function() {
		if (this.data.tokenNum >= 6) {
			wx.showToast({
				title: '最多添加6个Token',
				icon: 'none',
				duration: 2000
			})
		}else {
			this.setData({
				tokenNum: this.data.tokenNum + 1
			});
		}
	},
	/**
	 * 生成按钮-点击生成
	 */
	submit:function(options) {
		var canvasData = options.detail.value;
		if (this.data.tokenLogo.length < this.data.tokenNum) {
			wx.showModal({
				title: '提示',
				showCancel:false,
				content: '每个TokenLogo都必须要上传',
			})
			return false;
		}
		canvasData['headImg'] = this.data.headImg;
		//设备类型 安卓还是苹果
		if(this.data.nowPage % 2 == 0) {
			canvasData['equipmentType'] = 'android';
		}else {
			canvasData['equipmentType'] = 'iphone';
		}

		canvasData['tokenNum'] = this.data.tokenNum;
		canvasData['tokenLogo'] = this.data.tokenLogo;
		wx.setStorageSync('canvasData', canvasData);
		//参数校验
		if (!app.checkOptions(canvasData)) {
			return false;
		}

		wx.navigateTo({
			url: 'success'
		})
	},
	/**
	 * 调用系统相册上传图片
	 */
	albumUp:function(options){
		let that = this;
		wx.chooseImage({
			count: 1, // 默认9
			sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
			success: function (res) {
				var tempFilePaths = res.tempFilePaths[0];
				if(that.data.upImgType == 'headImg') {
					that.setData({
						headImg: tempFilePaths
					});
				}else if(that.data.upImgType == 'logo') {
					//上传tokenLogo 
					var tokenLogo =  that.data.tokenLogo;
					tokenLogo[that.data.tokenLogoNth] = tempFilePaths;
					that.setData({
						tokenLogo: tokenLogo				
					})
				
				}
				that.hiddenShade();
			}
		})
	},
	randomUp:function(options){
		let that = this;
		var suijishu = parseInt(Math.random() * (122 - 66 + 1) + 62, 10);
		var suijiImg = "http://wechat-program.maggie.vip/images/headImg/WechatIMG"+suijishu+".jpeg";
		wx.getImageInfo({
			src: suijiImg,
			success: function (res) {
				suijiImg = res.path;
				if (that.data.upImgType == 'headImg') {
					that.setData({
						headImg: suijiImg
					});
				} else if (that.data.upImgType == 'logo') {
					//上传tokenLogo 
					var tokenLogo = that.data.tokenLogo;
					tokenLogo[that.data.tokenLogoNth] = suijiImg;
					that.setData({
						tokenLogo: tokenLogo
					})
				}
			}
		})
		
		that.hiddenShade();
	},
	getTokenPrice: function (options) {
		let that = this;
		var index = options.target.dataset.index;
		var value = options.detail.value;
		var tokenPrice = that.data.tokenPrice;
		wx.showLoading({
			title: '自动加载行情中...',
		})
		wx.request({
			url: app.globalData.siteroot + 'test.php',
			dataType: 'json',
			data: {
				name: value
			},
			method: "GET",
			success: function (e) {
				tokenPrice['data' + index] = e.data;
				that.setData({
					tokenPrice: tokenPrice
				});
				wx.hideLoading()
			}
		})
	},
	//输入token名称自动转大写
	toUpperCase: function (options) {
		let that = this;
		var index = options.target.dataset.index;
		var value = options.detail.value;
		var tokenName = that.data.tokenName;
		tokenName['data' + index] = value.toUpperCase();
		that.setData({
			tokenName: tokenName
		});
		console.log(that.data.tokenName);
	}
})